package top.sdaily.storage.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.springframework.web.multipart.MultipartFile;

import top.sdaily.storage.Storage;

/**
 * Created by admin on 2017/3/20.
 */
public class FileUtil {

    public static String getExtension(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".")+1);
    }

    public static void saveFile(String newsRootPath, String filename,
                                File file) {
        saveFile(newsRootPath + filename, file);
    }

    public static void saveFile(String dest,
                                File file) {
        try {
            File newsFileRoot = new File(dest);
            if (!newsFileRoot.exists()) {
                newsFileRoot.mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(dest);
            FileInputStream fis = new FileInputStream(file);
            byte[] buf = new byte[1024];
            int len;
            while ((len = fis.read(buf)) > 0) {
                fos.write(buf, 0, len);
            }
            if (fis != null)
                fis.close();
            if (fos != null)
                fos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void saveFile(String dest,
                                MultipartFile file) {
        File destFile = new File(dest);
        if(!destFile.getParentFile().exists()){
            destFile.mkdirs();
        }
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    public static Storage generationFileInfo(Storage storage){
        String hash;
        String firstFolder;
        String secondFolder;
        String fileName;
        MessageDigest md5= null;
        try {
            MultipartFile file = storage.getFile();
            md5 = MessageDigest.getInstance("MD5");
            md5.update(file.getBytes());
            BigInteger bi = new BigInteger(1, md5.digest());

            hash=bi.toString(16);
            firstFolder = hash.substring(0,2);
            secondFolder = hash.substring(2,4);
            fileName = hash.substring(4,16);

            storage.setFileName(fileName);
            storage.setExtension(getExtension(file.getName()));
            storage.setAccessPath(firstFolder + File.pathSeparator + secondFolder + File.pathSeparator + fileName);
            storage.setFileSize(file.getSize());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
        }

        return storage;
    }

}
